<?php
namespace app\admin\controller;

use app\common\lib\Status as StatusLib;
use think\facade\Filesystem;

class Image extends AdminBase {
    public function upload() {
        if ( ! $this->request->isPost()){
            return show(config('status.error'), '请求不合法');
        }
        $file = $this->request->file('file');

        // 注意事项
        // 1.上传图片类型需要判断png gif jpg
        // 2.文件大小限制 600K
        // 3.图片长宽的限制

        // 默认上传成功是在runtime/storage/upload目录里面
//        $filename = Filesystem::putFile('upload', $file);
        // 默认上传成功是在public/storage/upload/image目录里面,这个可以在config/filesystem.php 文件中调整
        $filename = Filesystem::disk('public')->putFile('upload/image', $file);
        if (!$filename){
            return show(config('status.error'), '上传图片失败');
        }
        $imageUrl = '/storage/'.$filename;
        return show(config('status.success'), '上传图片成功', $imageUrl);
    }

    public function layUpload() {
        if ( ! $this->request->isPost()){
            return show(config('status.error'), '请求不合法');
        }
        $file = $this->request->file('file');
        $filename = Filesystem::disk('public')->putFile('upload/image', $file);
        if (!$filename){
            return json(['code'=>1, 'data'=>[]], 200);
        }
        $result = ['code'=>0, 'data'=>['src'=>'/storage/'.$filename]];
        return json($result, 200);
    }
}